package models

import (
	"gorm.io/gorm"
	"srv/house"
)

type User struct {
	gorm.Model
	Phone               string `gorm:"type:char(11);not null;comment:'账号'"`
	UserName            string `gorm:"type:varchar(30);not null;comment:'用户姓名'"`
	UserCard            string `gorm:"type:char(18);not null;comment:'身份证号'"`
	UserCardStraight    string `gorm:"type:varchar(255);not null;comment:'身份证正面'"`
	UserCardReverseSide string `gorm:"type:varchar(255);not null;comment:'身份证反面'"`
	Status              int    `gorm:"type:int;default: 1;comment:'状态: 1-待审核 2-审核通过 3-审核未通过'"`
}

func (u *User) FindUser(phone string) error {
	return DB.Debug().Where("phone = ?", phone).First(&u).Error
}
func (u *User) FirstUser(userId int) error {
	return DB.Debug().Where("id = ?", userId).First(&u).Error
}
func (u *User) CreateUser() error {
	return DB.Debug().Create(&u).Error
}
func (u *User) UpUser(userId int) error {
	return DB.Debug().Where("id = ?", userId).Updates(&u).Error
}
func (u *User) ListUser() (userIng []*house.UserList) {
	var newUser []User
	DB.Debug().Find(&newUser)
	for _, user := range newUser {
		list := house.UserList{
			Phone:               user.Phone,
			UserName:            user.UserName,
			UserCard:            user.UserCard,
			UserCardStraight:    user.UserCardStraight,
			UserCardReverseSide: user.UserCardReverseSide,
			Status:              int64(user.Status),
		}
		userIng = append(userIng, &list)
	}
	return
}
